package cn.itsy.service.dept;

import cn.itsy.dao.dept.DeptDao;
import cn.itsy.pojo.Dept;
import cn.itsy.pojo.EmpAndDept;
import cn.itsy.pojo.JobAndEmpAndDept;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service(value = "deptServiceimpl")
public class DeptServiceimpl implements DeptService {
    /**
     * deptDao:持久层引用
     * 如果操作数据库不等于0直接返回true否者false
     */
    @Resource(name = "deptDaoimpl")
    private DeptDao deptDao;

    @Override
    public boolean insertDept(String sql, Dept dept) {
        Integer integer = this.deptDao.insertDept(sql, dept);
        if (integer != 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<Dept> queryAll(String sql) {
        List<Dept> depts = this.deptDao.queryAll(sql);
        return depts;
    }

    @Override
    public Dept queryDeptById(String sql, Integer id) {
        List<Dept> depts = this.deptDao.queryDeptById(sql, id);
        if (!depts.isEmpty() & depts.size() == 1) {
            return depts.get(0);
        }
        return null;
    }

    @Override
    public boolean updateDeptById(String sql, Dept dept, String ck) {
        Integer integer = 0;
        if (ck.equals("success")) {
            integer = this.deptDao.updateDeptById(sql, dept);
        }
        if (ck.equals("error")) {
            sql = "update dept set dbian = ? where id=? ";
            integer = this.deptDao.updateDeptById(sql, dept.getDbian(), dept.getId());
        }

        if (integer != 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean updateDeptById(String sql, Integer pid, Integer id) {
        Integer integer = this.deptDao.updateDeptById(sql, pid, id);
        if (integer != 0) {
            return true;
        }
        return false;
    }

    @Override
    public void deleteDeptById(String sql, Integer idValue) {
        this.deptDao.deleteDeptById(sql, idValue);
    }

    @Override
    public List<EmpAndDept> queryEmpAndDeptById(String sql, Integer depart_id) {
        List<EmpAndDept> empAndDepts = this.deptDao.queryEmpAndDeptById(sql, depart_id);
        return empAndDepts;
    }

    @Override
    public List<JobAndEmpAndDept> queryDeptsById(String sql, Integer... id) {
        List<JobAndEmpAndDept> depts = this.deptDao.queryDeptsById(sql, id);
        return depts;
    }

    @Override
    public Dept queryInsertNewId(String sql) {
        List<Dept> integers = this.deptDao.queryLastID(sql);
        if (!integers.isEmpty()) {
            return integers.get(0);
        }
        return null;
    }

    @Override
    public boolean updateEmpAndJobById(String sql, Integer... id) {
        boolean b = this.deptDao.updateEmpAndJobById(sql, id[2], id[0], id[1]);
        return b;
    }

    @Override
    public boolean updateJobById(String sql, Integer... id) {
        boolean b = this.deptDao.updateJobById(sql, id[2], id[0], id[1]);
        return b;
    }

    @Override
    public void deleteDeptByIds(String sql, Integer... id) {
        this.deptDao.deleteDeptByIds(sql, id[0], id[1]);
    }

}
